//
//  ViewController.m
//  Qihuo
//
//  Created by Vencoo on 13-9-25.
//  Copyright (c) 2013年 Vencoo. All rights reserved.
//
#import "Appinfomation.h"
#import "JSON.h"
#import "ViewController.h"
#import "FirstViewController.h"
#import "MyTextField.h"
#import "NewsViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController ()
{
    UITextField *userName;
    UITextField *passWord;
    int clickCount;
    UIImage *memImage;
    ASIFormDataRequest *loginRequest;
    FirstViewController *FV;

}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIImageView *backGround = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"首页底"]];
    backGround.frame = CGRectMake(0, 0, 1024, 768);
    [self.view addSubview:backGround];
    
    UIImage *littleImage = [UIImage imageNamed:@"登陆框"];
    UIImageView *loginBackGroundView = [[UIImageView alloc]initWithImage:littleImage];
    loginBackGroundView.frame = CGRectMake(75, 165, littleImage.size.width, littleImage.size.height);
    [self.view addSubview:loginBackGroundView];
    
    UIImage *userImage = [UIImage imageNamed:@"输入框-1"];
    UIImage *passwordImage = [UIImage imageNamed:@"输入框-2"];
    UIImageView *userView = [[UIImageView alloc]initWithImage:userImage];
    userView.frame = CGRectMake(loginBackGroundView.frame.origin.x + (loginBackGroundView.frame.size.width - userImage.size.width)/2, loginBackGroundView.frame.origin.y + (loginBackGroundView.frame.size.width - userImage.size.width)/2, userImage.size.width , userImage.size.height);
    UIImageView *passwordView = [[UIImageView alloc]initWithImage:passwordImage];
    passwordView.frame = CGRectMake(userView.frame.origin.x, userView.frame.origin.y + userImage.size.height + 10, passwordImage.size.width , passwordImage.size.height);
    [self.view addSubview:passwordView];
    [self.view addSubview:userView];

   
//    UILabel *label = [UILabel new];
//    label.font = [UIFont systemFontOfSize:22];
//    label.text = @"请输入用户名";
//    label.textColor =
    userName = [[UITextField alloc]initWithFrame:CGRectMake(userView.frame.origin.x + 95, userView.frame.origin.y + (userImage.size.height - userView.frame.size.height + 10)/2, userView.frame.size.width - 95 ,userView.frame.size.height - 10)];
    userName.keyboardType = UIKeyboardTypeAlphabet;
    if ([[NSUserDefaults standardUserDefaults]objectForKey:@"user"]) {
        userName.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"user"];
    }
    userName.font = [UIFont systemFontOfSize:32];
    userName.textColor = [UIColor colorWithRed:34/0xff green:60/0xff blue:82/0xff alpha:1];
    userName.placeholder = @"请输入用户名";
    userName.backgroundColor = [UIColor clearColor];
    userName.contentVerticalAlignment = 0;
    
    passWord = [[UITextField alloc]initWithFrame:CGRectMake(passwordView.frame.origin.x + 95, passwordView.frame.origin.y + (passwordImage.size.height - passwordView.frame.size.height+10)/2, passwordView.frame.size.width-95, passwordView.frame.size.height-10)];
    
    if ([[NSUserDefaults standardUserDefaults]objectForKey:@"password"]) {
        passWord.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"password"];
    }
    //userName sette
    passWord.font = [UIFont systemFontOfSize:32];
    passWord.placeholder = @"请输入密码";
    passWord.textColor = [UIColor colorWithRed:34/0xff green:60/0xff blue:82/0xff alpha:1];
    [passWord setSecureTextEntry:YES];
    passWord.backgroundColor = [UIColor clearColor];
    passWord.contentVerticalAlignment = 0;
    
    UIButton *login = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    UIImage *logImage = [UIImage imageNamed:@"1"];
    login.frame = CGRectMake(passwordView.frame.origin.x, passwordView.frame.origin.y + passwordView.frame.size.height + 10, logImage.size.width, logImage.size.height);
    [login setBackgroundImage:logImage forState:UIControlStateNormal];
    [login addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *resert = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    UIImage *resertImage = [UIImage imageNamed:@"2"];
    resert.frame = CGRectMake(login.frame.origin.x + login.frame.size.width + loginBackGroundView.frame.size.width - userImage.size.width - 10, login.frame.origin.y, resertImage.size.width, resertImage.size.height);
    [resert setBackgroundImage:resertImage forState:UIControlStateNormal];
    [resert addTarget:self action:@selector(resert) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:userName];
    [self.view addSubview:passWord];
    [self.view addSubview:login];
    [self.view addSubview:resert];

    UIButton *memPass = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [memPass addTarget:self action:@selector(memePass:) forControlEvents:UIControlEventTouchUpInside];
    NSLog(@"---%@===",[[NSUserDefaults standardUserDefaults]objectForKey:@"click"]);
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"click"]intValue] == 1) {
       memImage = [UIImage imageNamed:@"选择框-2"];
    }else{
        memImage = [UIImage imageNamed:@"选择框-1"];
    }
    [memPass setBackgroundImage:memImage forState:UIControlStateNormal];
    memPass.frame = CGRectMake(login.frame.origin.x, loginBackGroundView.frame.origin.y + loginBackGroundView.frame.size.height + 30, memImage.size.width, memImage.size.height);
    [self.view addSubview:memPass];
    
    UIImage *textImage = [UIImage imageNamed:@"3"];
    UIImageView *textView = [[UIImageView alloc]initWithImage:textImage];
    textView.frame = CGRectMake(memPass.frame.origin.x + memPass.frame.size.width + 20, memPass.frame.origin.y + 8, textImage.size.width, textImage.size.height);
    [self.view addSubview:textView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma - mark button action
- (void)login
{
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"click"]integerValue] == 1) {
        [[NSUserDefaults standardUserDefaults] setObject:userName.text forKey:@"user"];
        [[NSUserDefaults standardUserDefaults] setObject:passWord.text forKey:@"password"];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }else{
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"password"];
        [[NSUserDefaults standardUserDefaults]synchronize];
    }
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
    if (userName.text.length == 0) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告！" message:@"请输入用户名！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [alert show];
    }else if (passWord.text.length == 0){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告！" message:@"请输入密码！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [alert show];
    }else{
//        ====================登陆==============================
        MBProgressHUD *mpb =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
        mpb.delegate = self;
        [mpb setDimBackground:YES];
        NSURL *loginUrl = [NSURL URLWithString:loginIP];
        loginRequest = [[ASIFormDataRequest alloc] initWithURL:loginUrl];
    
        [loginRequest setPostValue:[NSString stringWithFormat:@"%@",userName.text] forKey:@"user"];
        [loginRequest setPostValue:[NSString stringWithFormat:@"%@",passWord.text] forKey:@"password"];
        [loginRequest startAsynchronous];
        loginRequest.delegate = self;
        [loginRequest setDidFinishSelector:@selector(loginSuccess:)];
        [loginRequest setDidFailSelector:@selector(loginFail:)];
        
    }
}
- (void)loginSuccess:(id)sender
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    NSString *response = [loginRequest responseString];
    NSDictionary *responseDic = [response JSONValue];
    _DataDic = [responseDic objectForKey:@"data"];
    if (_DataDic == nil) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告！" message:@"网络无法连接！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [alert show];
    }else if ([_DataDic isEqual:[NSString stringWithFormat:@"NONE"]]) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告！" message:@"用户名不存在！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [alert show];
    }else if ([_DataDic isEqual:[NSString stringWithFormat:@"FAILURE"]]){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告！" message:@"用户密码错误！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
        [alert show];
    }else {
        
        
        /*==========================测试========================*/
        
        //        if ([userName.text isEqualToString: @"Admin"]) {
        //            _testDepart = 1 ;
        //        }else if ([userName.text isEqualToString:@"System"]){
        //            _testDepart = 2;
        //        }else if([userName.text isEqualToString:@"Sys"]){
        //            _testDepart = 3;
        //        }
        //        NSLog(@"------%d---------",_testDepart);
        /*============测试=============*/
        FV = [FirstViewController new];
        FV.delegate = self;
        [Appinfomation shareIstance].loginDic = _DataDic;
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:FV];
        if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"click"]integerValue] == 0){
            passWord.text = nil;
        }
        [self presentViewController:nav animated:YES completion:nil];
    }

}
- (void)loginFail:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告！" message:@"网络无法连接！" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
    [alert show];
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}
- (void)resert
{
    userName.text = nil;
    passWord.text = nil;
//    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"password"];
}
- (void)memePass:(UIButton*)btn
{
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"click"]intValue]==1) {
        _isTouched = NO;
    }else if([[[NSUserDefaults standardUserDefaults]objectForKey:@"click"]intValue] == 0){
        _isTouched = YES;
    }
    
    if (_isTouched) {
        [btn setBackgroundImage:[UIImage imageNamed:@"选择框-2"] forState:UIControlStateNormal];
        [[NSUserDefaults standardUserDefaults] setObject:userName.text forKey:@"user"];
        [[NSUserDefaults standardUserDefaults] setObject:passWord.text forKey:@"password"];
        [[NSUserDefaults standardUserDefaults]setObject:[NSString stringWithFormat:@"%d",_isTouched] forKey:@"click"];
        _isTouched = NO;
        [[NSUserDefaults standardUserDefaults]synchronize];
    }
    else
    {
        [btn setBackgroundImage:[UIImage imageNamed:@"选择框-1"] forState:UIControlStateNormal];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"password"];
        [[NSUserDefaults standardUserDefaults]setObject:[NSString stringWithFormat:@"%d",_isTouched] forKey:@"click"];
        _isTouched = YES;
        [[NSUserDefaults standardUserDefaults]synchronize];

    }
    NSLog(@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"click"]);
}
@end
